Programming - Loops Continue

OPENING QUESTIONS: Please take a look at this rather 'elegant' code developed by our own sailing master Finn HERE. A bunch of us do have some programming background so using the logical AND, OR and NOT operators makes sense to us. Having said that, we've also got rookies who haven't done coding before and such stuff makes much less sense.

So, please let's pair a rookie in your group with a Sensei to work through that code to identify the logical operators and how they are used to great effect in that code.

LEARNING TARGET: I will be able to use the AND and OR logic operators in a basic piece of code during today's class.

COURSE DOCUMENT: Ver 1.70 is HERE

WORK O' THE DAY

We learned about the logical operators AND, OR and NOT during our discussions of boolean math last semester. Recall that:

  • 0 represents False
  • 1 represents True

Please complete the following table (solo) and then review with your team:

Statement
Resoning
Code
0 AND 0
Both must be true. However both are false so the result is false
0 && 0
1 AND 0
 
1 && 0
1 AND 0
   
1 AND 1
   
0 OR 0
 
0 || 0
1 OR 0
   
0 OR 1
 
0 || 1
1 OR 1
   
NOT (1 OR 1)    
NOT (1 AND 1)    
NOT (1 OR 0)    
NOT (1 AND 0)    

═══════════════════════════

Consider the following (kinda silly) code:

var x = getText("monthNum"); // Get input from the monthNum text box
var y = getText("dayNum"); // Get input from the dayNum text box

if (x == 5 && y == 10) {
console.log("You entered May 10th!!!!");
}

1) Why would you get docked a point on that code (it works by the way)

2) What is the logic there?

Now consider the following:

var x = getText("monthNum"); // Get input from the monthNum text box

var y = getText("dayNum"); // Get input from the dayNum text box

if (!(x == 5 && y == 10)) { console.log("At least one condition is false!"); } else { console.log("Both conditions are true!"); }

3) Senseis: Please explain why THIS works:

inputBox is a textBox with a default value of 0:

if (getText("inputBox"))

{ console.log("The value of inputbox has changed!"); }

 

Now please engage in some "sandbox" time playing with AND, OR and NOT operators.

Senseis, get sassy! (but not too extreme!)

Rookies, please revise your leap year code from yesterday to include logic operators